﻿using System.Collections.Generic;
using System.Linq;
using System;
using SDT.DataService.Contexts;
using SDT.DataService.Repositories;
using SDT.DataService;
using SDT.Libraries.Utilities;


namespace SDT.DataService.Services
{
    public partial class CategoryService : IService<Spider_Category>
    {
        #region Field
        
        private IRepository<Spider_Category> repository;
        #endregion

        #region Constructor
        public CategoryService()
            : this(new Repository<Spider_Category>())
        {
        }

        public CategoryService(IRepository<Spider_Category> repository)
        {
            this.repository = repository;
         
        }
        #endregion

        #region Method

        public int InsertCategory(int cid, string name)
        {
            CategoryContext contextObj = new CategoryContext();
            try
            {
                return contextObj.InsertCategory(cid, name);
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "CategoryService.InsertCategory", ex.Message));

            }
            finally { contextObj.Dispose(); }
            return 0;
        }

        public int UpdateCategory(int cateid, string name)
        {
            CategoryContext contextObj = new CategoryContext();
            try
            {
                return contextObj.UpdateCategory(cateid, name);
            }
            catch (Exception ex)
            {
                new LogException().Write(string.Format("  Module {0} --Msg {1} ", "NewsService.GetContentByPageID", ex.Message));

            }
            finally { contextObj.Dispose(); }
            return 0;
        }


        public void BuildCategoryTree(List<Spider_Category> catList, int nParentID, int nLevel)
        {
          
        }
        #endregion

        #region IService<Spider_Category> Members
        public IEnumerable<Spider_Category> FillAll(Func<Spider_Category, bool> exp)
        {
            try
            {
                return GetAll().Where<Spider_Category>(exp);
            }
            catch (Exception ex)
            {
                return null;

            }
        }

     
        public IQueryable<Spider_Category> GetAll()
        {
            try
            {
                return repository.GetAll();
            }
            catch (Exception ex)
            {
                return null;
            }
        }
        
        public bool Update(Spider_Category entity)
        {
            try
            {
                repository.Update(entity);
                return true;
            }
            catch
            {
                return false;

            }
        }

        public bool Create(Spider_Category entity)
        {
            try
            {
                entity.CreatedDate = DateTime.Now;
                repository.Create(entity);
                return true;
            }
            catch (Exception ex)
            {

                return false;

            }
        }

        public bool Delete(Spider_Category entity)
        {
            try
            {
               // entity.isDeleted = true;
                return Update(entity);
            }
            catch (Exception ex)
            {

                return false;
            }

        }

        public IQueryable<Spider_Category> GetAll(bool isDeleted,int parentId)
        {
            try
            {
                return repository.GetAll();
            }
            catch (Exception ex)
            {
                return null;
            }
        }


        #endregion

    }
}
